﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace TARS.AdminCP
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_ItemCommand2(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.EditCommandName)
                RadGrid1.MasterTableView.IsItemInserted = false; //Close Insert Item if Update Item Open
            if (e.CommandName == RadGrid.InitInsertCommandName)
                RadGrid1.MasterTableView.ClearEditItems(); //Close Update Item if Insert Item Open
        }


        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //Set visible in Edit or Insert item
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem edit = (GridEditFormItem)e.Item;
                edit["ID"].Visible = false;
                edit["CreatedTime"].Visible = false;
            }
        }


        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            String ZoneName = (item["ZoneName"].Controls[0] as TextBox).Text;
            bool Active = (item["Active"].Controls[0] as CheckBox).Checked;

            Guid BranchesZoneID = Guid.NewGuid();

            t_BrachesZone newBranchesZone = new t_BrachesZone();
            newBranchesZone.ID = BranchesZoneID;
            newBranchesZone.ZoneName = ZoneName;
            newBranchesZone.CreatedTime = DateTime.Now;
            newBranchesZone.Active = Active;

            using (var db = new TARSDelivery_EprojectEntities())
            {
                db.t_BrachesZone.Add(newBranchesZone);
                db.SaveChanges();
            }
            RadGrid1.MasterTableView.ClearEditItems();
            RadGrid1.MasterTableView.ClearSelectedItems();
            RadGrid1.MasterTableView.ClearChildEditItems();
        }

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            String ZoneName = (item["ZoneName"].Controls[0] as TextBox).Text;
            bool Active = (item["Active"].Controls[0] as CheckBox).Checked;
            Guid BranchesZoneID = new Guid((item["ID"].Controls[0] as TextBox).Text);

            using (var db = new TARSDelivery_EprojectEntities())
            {
                t_BrachesZone editBranchesZone = db.t_BrachesZone.First(x => x.ID == BranchesZoneID);
                editBranchesZone.ZoneName = ZoneName;
                editBranchesZone.Active = Active;
                db.SaveChanges();
            }
            RadGrid1.MasterTableView.ClearEditItems();
            RadGrid1.MasterTableView.ClearSelectedItems();
            RadGrid1.MasterTableView.ClearChildEditItems();
        }
    }
}